package com.wgts.wgts.netmodle;

import android.util.Log;

import com.wgts.wgts.MoHuApplication;

import java.io.File;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import ren.yale.android.retrofitcachelib.intercept.CacheForceInterceptorNoNet;
import ren.yale.android.retrofitcachelib.intercept.CacheInterceptorOnNet;

public class AppOkHttpClient {

    private AppOkHttpClient() {
    }

    private static class SingletonHolder{
        private static final AppOkHttpClient _INSTANCE = new AppOkHttpClient();
    }

    public static AppOkHttpClient getInstance(){
        return SingletonHolder._INSTANCE;
    }

    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
        @Override
        public void log(String message) {
            //打印retrofit日志
            Log.i("RetrofitLog","retrofitBack ======================= "+message);
        }
    });


    private static final long DEFAULT_TIMEOUT = 10;


    public OkHttpClient getOkHttpClient(){

        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        //定制OkHttp
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
        builder.writeTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
        builder.addInterceptor(loggingInterceptor);

        //设置缓存
        //File httpCacheDirectory = new File(SDCardUtils.getRootDirectoryPath(),"这里是你的网络缓存存放的地址");
        //builder.cache(new Cache(httpCacheDirectory,10*1024*1024));

        //builder.addInterceptor(new CacheForceInterceptorNoNet());
        //builder.addNetworkInterceptor(new CacheInterceptorOnNet());

        //int cacheSize = 40 * 1024 * 1024;
        //File cacheDirectory = new File(MoHuApplication.getInstance().getCacheDir(), "mohuCache");
        //Cache cache = new Cache(cacheDirectory, cacheSize);

        //return builder.cache(cache).build();
        return builder.build();
    }
}
